package com.bndg.browser.app

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.annotation.Nullable
import com.bndg.browser.ActivityManager
import com.bndg.browser.BuildConfig
import com.bndg.browser.util.Trace
import timber.log.Timber.*
import timber.log.Timber.Forest.plant


class MyApplication : Application(), Application.ActivityLifecycleCallbacks {
    var foregroundActivityCount: Int = 0
    override fun onCreate() {
        super.onCreate()
        // Activity 栈管理初始化
        ActivityManager.getInstance().init(this)
        initSdk()
    }

    private fun initSdk() {
        plant(object : DebugTree() {
            @Nullable
            override fun createStackElementTag(element: StackTraceElement): String? {
                return "(" + element.fileName + ":" + element.lineNumber + ")[" + Thread.currentThread().name + "]"
            }
        })
        Trace.getConfig()
            .setLogSwitch(BuildConfig.DEBUG) // 设置log总开关，默认开
            .setGlobalTag(">>>>") // 设置log全局标签，默认为空
            // 当全局标签不为空时，我们输出的log全部为该tag，
            // 为空时，如果传入的tag为空那就显示类名，否则显示tag
            .setLog2FileSwitch(false) // 打印log时是否存到文件的开关，默认关
            .setBorderSwitch(true) // 输出日志是否带边框开关，默认开
    }


    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        // 可选：记录Activity创建
    }

    override fun onActivityStarted(activity: Activity) {
        foregroundActivityCount++
        // 应用进入前台时，foregroundActivityCount会增加
    }

    override fun onActivityResumed(activity: Activity) {
        // 可选：记录Activity恢复
    }

    override fun onActivityPaused(activity: Activity) {
        // 可选：记录Activity暂停
    }

    override fun onActivityStopped(activity: Activity) {
        foregroundActivityCount--
        // 应用进入后台时，foregroundActivityCount会减到0
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        // 可选：保存Activity的状态
    }

    override fun onActivityDestroyed(activity: Activity) {
        // 可选：记录Activity销毁
    }
}
